local Msg = require("core.Msg")
local ActDefine = require("activity.Define")
local DateActExcel = require("excel.DateActivity")
local Grid = require("bag.Grid")
local ItemDefine = require("bag.ItemDefine")
local HolidayGiftExcel = DateActExcel.holidayGift
local DateActFW = require("activity.DateActFW")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")

HOLIDAYGIFT_DBDATA = HOLIDAYGIFT_DBDATA or nil

function init()
    HOLIDAYGIFT_DBDATA = DateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT)
end

function onGetReward(human)
    if not HOLIDAYGIFT_DBDATA then
        return
    end

    if HOLIDAYGIFT_DBDATA[human._id] == 1 then
        return
    end

    local nRuleID = DateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT)
    if nRuleID and HolidayGiftExcel[nRuleID] then
        local nHolidayGiftConfig = HolidayGiftExcel[nRuleID]
        local nGiftList = nHolidayGiftConfig.giftlist
        if nGiftList then

            HOLIDAYGIFT_DBDATA[human._id] = 1
            DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT, HOLIDAYGIFT_DBDATA)

            local nSend2Ldata = {
                type = 6,
                data = {
                    giftlist = nGiftList,
                }
            }
            InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_ACT, nSend2Ldata)
            onProecss(human)
        end
    end
end

function onActEnd()
    HOLIDAYGIFT_DBDATA = nil
    DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT, HOLIDAYGIFT_DBDATA)
end

function onActReset()
    HOLIDAYGIFT_DBDATA = {} 
    DateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT, HOLIDAYGIFT_DBDATA)
end


function onActRunning()
    HOLIDAYGIFT_DBDATA = DateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT)
    if not HOLIDAYGIFT_DBDATA then
        HOLIDAYGIFT_DBDATA = {}
    end
end

function get_grid(itemID, itemNum, itemColor, itemBind)
	local grid = Grid.create(nil, itemID, itemNum, itemBind, ItemDefine.qualityOut[itemColor])
	return grid
end

function onProecss(human)
    local nSendMsg = Msg.gc.GC_DATEACT_HOLIDAYGIFT_QUREY
    nSendMsg.giftlist[0] = 0
    local nRuleID = 0
    nRuleID = DateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT)
    if not nRuleID or not HolidayGiftExcel[nRuleID] then
        return
    end

    local nHolidayGiftConfig = HolidayGiftExcel[nRuleID]
    nSendMsg.status = HOLIDAYGIFT_DBDATA[human._id] and 1 or 0 
    nSendMsg.holidayTitle = nHolidayGiftConfig.titleid 
    nSendMsg.holidayIcon = nHolidayGiftConfig.iconid 
    local nGiftList = nHolidayGiftConfig.giftlist
    for k, v in ipairs(nGiftList) do
        nSendMsg.giftlist[0] = nSendMsg.giftlist[0] + 1
        local nRewardData = nSendMsg.giftlist[nSendMsg.giftlist[0]]
        local grid = get_grid(v[1], v[2], nil, 0)
        Grid.makeItem(grid, nRewardData)
        if k >= 4 then
            break
        end
    end

    Msg.send(nSendMsg, human.fd)
end

DateActFW.onRegisterCallBack(ActDefine.OPERATEACT_TYPE_HOLIDAYGIFT, nil, onActRunning, onActEnd, nil)

